package com.sugon.controller.basics;

import com.sugon.annotation.AutoLog;
import com.sugon.entity.StoreHouseEntity;
import com.sugon.service.StoreHouseService;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.sugon.utils.ShiroUtils.getUserId;

/**
 * 仓库Controller
 *
 * @author sugon
 * @date 2019-07-12 08:52:28
 */
@Controller
@RequestMapping("storehouse")
public class StoreHouseController {
    @Autowired
    private StoreHouseService storeHouseService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("storehouse:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {

        params.put("delFlag", 1);
        //查询列表数据
        Query query = new Query(params);

        List<StoreHouseEntity> storeHouseList = storeHouseService.queryList(query);
        int total = storeHouseService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(storeHouseList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 仓库树
     */
    @RequestMapping("/allList")
    @ResponseBody
    public R allList(@RequestParam Map<String, Object> params) {
        //查询列表数据
        List<StoreHouseEntity> storeHouseList = storeHouseService.allList(params);
        return R.ok().put("list", storeHouseList);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("storehouse:info")
    @ResponseBody
    public R info(@PathVariable("id") String id) {
        StoreHouseEntity storeHouse = storeHouseService.queryObject(id);

        return R.ok().put("storeHouse", storeHouse);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("storehouse:save")
    @ResponseBody
    public R save(@RequestBody StoreHouseEntity storeHouse) {
        storeHouse.setCreateUser(getUserId());
        storeHouse.setCreateDate(new Date());
        storeHouseService.save(storeHouse);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("storehouse:update")
    @ResponseBody
    public R update(@RequestBody StoreHouseEntity storeHouse) {
        storeHouseService.update(storeHouse);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("storehouse:delete")
    @ResponseBody
    public R delete(@RequestBody String[] ids) {
        for (String id : ids) {
            // 校验仓库是否存在。如果不存在，则直接跳过
            StoreHouseEntity storeHouse = storeHouseService.queryObject(id);
            if (storeHouse == null) {
                continue;
            }
            storeHouseService.delete(id);
        }
        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<StoreHouseEntity> list = storeHouseService.queryList(params);

        return R.ok().put("list", list);
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAllByModuleType")
    @ResponseBody
    public R queryAllByModuleType(@RequestParam Map<String, Object> params) {
        List<StoreHouseEntity> list = storeHouseService.queryList(params);

        return R.ok().put("list", list);
    }

    /**
     * 获取条数
     */
    @RequestMapping("/count")
    @ResponseBody
    public R count(@RequestParam Map<String, Object> params) {
        int total = storeHouseService.queryTotal(params);
        return R.ok().put("total", total);
    }
}
